//
//  InsuHeadBigV.m
//  carcareIOS
//
//  Created by 王然 on 16/7/26.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "InsuHeadBigV.h"
#import "SystemHelper.h"
#import "UmengMgr.h"
@interface InsuHeadBigV()

@property (nonatomic , strong) UILabel * headTitleL;//大cell上面的标题 我的保单号码
@property (nonatomic , strong) UILabel * headPoliyHolderNumL;//大cell上面的副标题 保单号码
@property (nonatomic , strong) UILabel * centerLeftTitleL;//紧急联络电话
@property (nonatomic , strong) UIImageView * centerLeftIconIV;//紧急联络电话Icon
@property (nonatomic , strong) UILabel * centerLeftNumL;//紧急联络电话号码

@property (nonatomic , strong) UILabel * centerRightTitleL;//保险总金额
@property (nonatomic , strong) UILabel * centerRightNumL;//保险总金额num

@property (nonatomic , strong) UILabel * bottomLeftL;
@property (nonatomic , strong) UILabel * bottomDetaiL;//查看详细保单信息的
@property (nonatomic , strong) UIImageView * rightImageV;


@property (nonatomic , copy) void(^bottomCellClick)();

@end
@implementation InsuHeadBigV

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
    }
    return self;
}
- (void)createUI
{
    CGFloat labelX = 15 * __MAIN_RATIO_375;
    CGFloat labelH = 20;
    UIView * headBackV = [[[UIView viewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, BIG_CELL_HEAD)] setupOnView:self] resetBackgroundColor:[Theme mainNavColor]];
    
    self.headTitleL = [[[UILabel labelWithFrame:CGRectMake(labelX, 10, __MAIN_WIDTH - labelX * 2, labelH) font:FONT_DEFAULT_Light(13) text:@"我的保单号码" textColor:COLOR_HEXSTRING(@"#AED6F5")] setupOnView:headBackV] labelResetTextAlignment:NSTextAlignmentCenter];
    
    self.headPoliyHolderNumL = [[[UILabel labelWithFrame:CGRectMake(labelX, self.headTitleL.bottom + 15, __MAIN_WIDTH - labelX * 2, labelH) font:FONT_DEFAULT_Light(25) text:@"" textColor:[UIColor whiteColor]] setupOnView:headBackV] labelResetTextAlignment:NSTextAlignmentCenter];
    
    
    UIView * centerLeftV = [[UIView viewWithFrame:CGRectMake(0, headBackV.bottom, __MAIN_WIDTH / 2, BIG_CELL_CENTER)] setupOnView:self];
    CGFloat smallLX = 7;
    CGFloat smallLW = centerLeftV.width - smallLX * 2;
    self.centerLeftTitleL = [[[UILabel labelWithFrame:CGRectMake(smallLX , 18, smallLW, labelH) font:FONT_DEFAULT_Light(13) text:@"紧急联络电话" textColor:COLOR_HEXSTRING(@"#B0B0B0")] setupOnView:centerLeftV] labelResetTextAlignment:NSTextAlignmentCenter];
    UIImage * leftIconImage = ImageNamed(@"insuHeadCellCallImage");
    self.centerLeftIconIV = [[UIImageView imageViewWithFrame:CGRectMake(0, self.centerLeftTitleL.bottom + 10, leftIconImage.size.width, leftIconImage.size.height) image:leftIconImage] setupOnView:centerLeftV];
    self.centerLeftNumL = [[[UILabel labelWithFrame:CGRectMake(smallLX, self.centerLeftTitleL.bottom + 10, smallLW, labelH)  font:FONT_DEFAULT_Light(20) text:@"" textColor:COLOR_HEXSTRING(@"#2090E6")] setupOnView:centerLeftV]labelResetTextAlignment:NSTextAlignmentCenter];
    __weak __typeof(self) wself = self;
    [[UIButton buttonEmptyWithFrame:centerLeftV.bounds click:^{
        if([self.centerLeftNumL.text isNotNil])
        {
            [SYSTEMHELPER callPhone:wself.centerLeftNumL.text];
        }
    }] setupOnView:centerLeftV];
    
    
    UIView * centerRightV = [[UIView viewWithFrame:CGRectMake(centerLeftV.right, centerLeftV.top, centerLeftV.width, centerLeftV.height)] setupOnView:self];
    self.centerRightTitleL = [[[UILabel labelWithFrame:CGRectMake(smallLX, self.centerLeftTitleL.top, smallLW, labelH) font:FONT_DEFAULT_Light(13) text:@"保险总金额" textColor:COLOR_HEXSTRING(@"#B0B0B0")] setupOnView:centerRightV]labelResetTextAlignment:NSTextAlignmentCenter];
    self.centerRightNumL = [[[UILabel labelWithFrame:CGRectMake(smallLX, self.centerRightTitleL.bottom + 10, smallLW, labelH) font:FONT_DEFAULT_Light(20) text:@"" textColor:COLOR_HEXSTRING(@"#FF672B")] setupOnView:centerRightV]labelResetTextAlignment:NSTextAlignmentCenter];
    
    CGFloat lineH = 41;
    [[[UIView viewWithFrame:CGRectMake(centerLeftV.right, BIG_CELL_HEAD +  (BIG_CELL_CENTER - lineH) / 2, 0.5, lineH)] setupOnView:self] setBackgroundColor:COLOR_HEXSTRING(@"#D7D7D7")];
    
    UIView * bottomBackV = [[UIView viewWithFrame:CGRectMake(0, centerLeftV.bottom, __MAIN_WIDTH, BIG_CELL_BOTTOM)] setupOnView:self];
    self.bottomLeftL = [[UILabel labelWithFrame:CGRectMake(labelX, 0, 140, bottomBackV.height) font:FONT_DEFAULT_Light(14) text:@"查看详细保单信息" textColor:[Theme wordColor]] setupOnView:bottomBackV];
    UIImage * rightImage = ImageNamed(@"cell_right");
    self.rightImageV = [Factory imageViewWithCenter: CGPointMake(__MAIN_WIDTH - 25, BIG_CELL_BOTTOM / 2) image:rightImage onView:bottomBackV];
    self.bottomDetaiL = [[[UILabel labelWithFrame:CGRectMake(self.bottomLeftL.right, 0, __MAIN_WIDTH - self.bottomLeftL.right - (rightImage.size.width / 2 + 28), BIG_CELL_BOTTOM) font:FONT_DEFAULT_Light(14) text:@"" textColor:COLOR_HEXSTRING(@"#919191")] setupOnView:bottomBackV] labelResetTextAlignment:NSTextAlignmentRight];
    self.userInteractionEnabled = YES;
    UIButton * lookPolicyBtn = [[[UIButton buttonEmptyWithFrame:CGRectMake(0, 0, bottomBackV.width, bottomBackV.height) click:^{
        [UmengMgr event:ICZ_LookPolicyDetail];
        [[StatisticsMgr sharedInstance] addClickID:@"查看详细保单信息" description:lookPolicyBtn.description];
        if (wself.bottomCellClick) wself.bottomCellClick();
    }] setupOnView:bottomBackV] resetBackgroundColor:[UIColor clearColor]];
    
    [[[UIView viewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, 0.5)] setupOnView:bottomBackV] resetBackgroundColor:COLOR_HEXSTRING(@"#D7D7D7")];
    
}
- (void)reloadWithModel:(PolicyModel *)model bottomCellClick:(void(^)())bottomCellClick
{
    if (bottomCellClick) self.bottomCellClick = bottomCellClick;
    
    //改变是否可查看详情控制的
    [self.rightImageV setHidden:!model.canClickLookDetailPolicy];
    self.userInteractionEnabled = model.canClickLookDetailPolicy;
    self.bottomLeftL.text = model.canClickLookDetailPolicy ? @"查看详细保单信息" : @"保险到期日";
    UIImage * rightImage = ImageNamed(@"cell_right");
    self.bottomDetaiL.width = model.canClickLookDetailPolicy ? ( __MAIN_WIDTH - self.bottomDetaiL.left - (rightImage.size.width / 2 + 28)) : (__MAIN_WIDTH - self.bottomDetaiL.left - self.headTitleL.left);
    
    self.headPoliyHolderNumL.text = model.policyNo;
    self.centerRightNumL.text = [NSString stringWithFormat:@"%.0f",model.premium];
    //@"剩余23天到期"
    self.bottomDetaiL.text = model.expireDateStr;
    
    NSString * leftNumStr = model.phone;
    NSDictionary *attributes = @{NSFontAttributeName:self.centerLeftNumL.font};
    
    CGSize centerLeftNumLSize = [leftNumStr boundingRectWithSize:CGSizeMake(self.centerLeftTitleL.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
    
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:leftNumStr];
    [attr addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(0, leftNumStr.length)];
    self.centerLeftNumL.attributedText = attr;
    UIImage * leftIconImage = ImageNamed(@"insuHeadCellCallImage");
    CGFloat leftLabelAndIconX = 5;
    CGFloat leftLabelAndIconW = leftIconImage.size.width + leftLabelAndIconX + centerLeftNumLSize.width;
    CGFloat leftIconX = (__MAIN_WIDTH / 2 - leftLabelAndIconW) / 2;
    
    [self.centerLeftIconIV setOrigin:CGPointMake(leftIconX, self.centerLeftNumL.bottom - leftIconImage.size.height + 2)];
    [self.centerLeftNumL setFrame:CGRectMake(self.centerLeftIconIV.right + leftLabelAndIconX, self.centerLeftNumL.top, centerLeftNumLSize.width, self.centerLeftNumL.height)];
}
@end
